package com.dongdongsongcan.app.adapter;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.activity.LoginActivity;
import com.dongdongsongcan.app.control.ShoppingCarControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.MenuListData.MenuList;
import com.dongdongsongcan.app.db.ShoppingCarDao;
import com.dongdongsongcan.app.db.bean.ShoppingCarDBBean;
import com.dongdongsongcan.app.fragment.ShopDetailFoodlistFragment;
import com.prone.vyuan.widget.dpdialog.DialogTool;
import com.su.dolphin.adapter.DpBaseAdapter;
import com.su.dolphin.core.proxy.ImageLoader;
import com.su.dolphin.utils.LogUtil;
import com.su.dolphin.utils.StringUtil;
import com.su.dolphin.utils.ViewUtil;
import com.su.dolphin.utils.ui.TipsUtil;

public class FoodlistAdapter extends DpBaseAdapter<MenuList> {

    private ShopDetailFoodlistFragment mShopDetailFoodlistFragment;

    public FoodlistAdapter(Context context,
                           ShopDetailFoodlistFragment shopDetailFoodlistFragment) {
        super(context);
        mShopDetailFoodlistFragment = shopDetailFoodlistFragment;
    }

    @Override
    public int onGetItemLayout() {
        return R.layout.layout_food_item;
    }

    @Override
    public void onGetView(View convertView, int position) {
        final MenuList menuList = mDataList.get(position);
        findViewById(convertView, R.id.food_item_container).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TipsUtil.toast(menuList.getMenu_title());
            }
        });
        TextView food_item_name = findViewById(convertView, R.id.food_item_name);
        ImageView food_item_icon = findViewById(convertView,
                R.id.food_item_icon);
        TextView food_item_price = findViewById(convertView,
                R.id.food_item_price);
        ImageView food_item_add = findViewById(convertView, R.id.food_item_add);
        final TextView food_item_count = findViewById(convertView,
                R.id.food_item_count);


        food_item_price.setText(menuList.getMenu_price() + "元/份");
        food_item_name.setText(menuList.getMenu_title());
        ImageLoader.i().loadImage(food_item_icon, menuList.getMenu_pic_small());
        long time = System.currentTimeMillis();
        ShoppingCarDBBean shoppingCarDBBeanExist = ShoppingCarDao.findFirst(
                mShopDetailFoodlistFragment.getShop().getShop_id(), menuList.getMenu_id());
        LogUtil.write("query product cost time "
                + (System.currentTimeMillis() - time));
        int count = shoppingCarDBBeanExist == null ? 0 : shoppingCarDBBeanExist
                .getCount();

        food_item_count.setText(String.valueOf(count));

//        {
//            "menu_id": "5783",
//                "menu_number": "189",
//                "menu_title": "澳洲龙虾（斤）",
//                "menu_bag_price": "5",
//                "menu_price": "198.00",
//                "menu_unit": "份",
//                "unit": "份",
//                "menu_pic_small": "http://www.jcy24.com/upload/attatch/1/澳洲龙虾_1448262703_samll.jpg",
//                "menu_tian": "0",
//                "menu_la": "0",
//                "menu_zhong": 1,
//                "menu_weight": "1-2斤|2-3斤|3斤以上",
//                "menu_zuo": 1,
//                "menu_make": "蒜蓉|粉丝蒸|油泼|生吃",
//                "menu_cold": "2"
//        },

        food_item_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (menuList.getMenu_zhong() == 0 && menuList.getMenu_zuo() == 0) {
                    mShopDetailFoodlistFragment.addToShoppingCar(menuList,
                            0 + "", 0 + "");
//                    int count = StringUtil.parseInt(food_item_count
//                            .getText().toString().trim()) + 1;
//                    food_item_count.setText(count + "");
                } else {
                    showMakeDialog(menuList, food_item_count);

                }
            }
        });
        findViewById(convertView, R.id.food_item_delete).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
//                        int count = StringUtil.parseInt(food_item_count
//                                .getText().toString().trim()) - 1;
//                        if (count < 0) {
//                            count = 0;
//                        }
//                        food_item_count.setText(count + "");
                        mShopDetailFoodlistFragment
                                .deleteProductFromShoppigCar(menuList);
                    }
                });

    }

    public void showMakeDialog(final MenuList menuList,
                               final TextView food_item_count) {
        View view = ViewUtil.inflateView(R.layout.dialog_shopdetail_food_make,
                getContext());

        final RadioGroup shopdetail_foodmake = (RadioGroup) view.findViewById(R.id.shopdetail_foodmake);
        RadioGroup shopdetail_foodweight = (RadioGroup) view.findViewById(R.id.shopdetail_foodweight);

        String[] weights = new String[0], makes = new String[0];
        if (menuList.getMenu_zhong() == 1) {
            weights = menuList.getMenu_weight().split("\\|");
            for (int i = 0; i < weights.length; i++) {
                RadioButton radioButton = new RadioButton(getContext());
                if (i == 0)
                    radioButton.setChecked(true);
                radioButton.setText(weights[i]);
                radioButton.setId(i);
                shopdetail_foodweight.addView(radioButton);
            }
        } else {
            view.findViewById(R.id.shopdetail_foodweight_title).setVisibility(View.GONE);
            shopdetail_foodweight.setVisibility(View.GONE);
        }

        if (menuList.getMenu_zuo() == 1) {
            makes = menuList.getMenu_make().split("\\|");
            for (int i = 0; i < makes.length; i++) {
                RadioButton radioButton = new RadioButton(getContext());
                if (i == 0)
                    radioButton.setChecked(true);
                radioButton.setText(makes[i]);
                radioButton.setId(i);
                shopdetail_foodmake.addView(radioButton);
            }
        } else {
            shopdetail_foodmake.setVisibility(View.GONE);
            view.findViewById(R.id.shopdetail_foodmake_title).setVisibility(View.GONE);

        }

        final Dialog dialog = DialogTool.buildPlainDialog(getContext(), view);
        dialog.show();
        final String[] finalWeights = weights;
        final String[] finalMakes = makes;
        view.findViewById(R.id.shopdetail_foodmake_ok).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String make = finalMakes.length == 0 ? "" : finalMakes[shopdetail_foodmake.getCheckedRadioButtonId()];
                        String weight = finalWeights.length == 0 ? "" : finalWeights[shopdetail_foodmake.getCheckedRadioButtonId()];

                        mShopDetailFoodlistFragment.addToShoppingCar(menuList, make, weight);
                        int count = StringUtil.parseInt(food_item_count
                                .getText().toString().trim()) + 1;
                        food_item_count.setText(count + "");

                        dialog.dismiss();
                    }
                });
        view.findViewById(R.id.shopdetail_foodmake_cancel).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

    }
}
